package Leetcode.DayTest;

import java.util.Arrays;
import java.util.PriorityQueue;
import java.util.Scanner;

/**
 * @ClassName Test_shenzhouxinxi
 * @since: 2025/3/5 16:27
 * @auth: kirito
 * @description: 神州信息笔试
 **/
public class Test_shenzhouxinxi {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        int n = in.nextInt();
        int m= in.nextInt();
        //桌子
        int[] tables = new int[n];
        for (int i = 0; i < n; i++) {
            tables[i] = in.nextInt();
        }

        //客人  人数-金额
        int[][] guests = new int[m][2];
        for (int i = 0; i < m; i++) {
            guests[i][0] = in.nextInt();
            guests[i][1] = in.nextInt();
        }
        //按照容纳人数排序
        Arrays.sort(tables);
        //金额大到小  人数小到大
        Arrays.sort(guests, (a, b) ->
        {
            return a[1] != b[1] ? b[1] - a[1] : a[0] - b[0];
        });
        long sum = 0;
        boolean[] used = new boolean[n];

        for (int[] guest : guests) {
            int people = guest[0];
            int cost = guest[1];

            int index = func_2(tables, used, people);
            if (index != -1) {
                used[index] = true;
                sum += cost;
            }
        }
        System.out.println(sum);

    }
    //找第一个能容纳当前批次的桌子
    public static int func_2(int[] tables,boolean[] used,int people) {
        int left=0;
        int right = tables.length - 1;
        int res = -1;
        while (left <= right) {
            int mid = left + (right - left) / 2;
            if (tables[mid] >= people) {
                res = mid;
                right = mid - 1;
            } else {
                left = mid + 1;
            }
        }

        while (res != -1 && res < tables.length) {
            if (!used[res] && tables[res] >= people) {
                return res;
            }
            res++;
        }
        return -1;
    }

    /**
     * 输入一个数字，奇数位置变1  偶数位置变0
     * @param num
     * @return
     */
    public static int func(int num) {
        int res = 0;
        int mult = 1;
        while (num > 0) {
            int digit = num % 10;
            if (digit % 2 != 0) {
                res += mult;
            }
            mult *= 10;
            num /= 10;
        }
        return res;
    }
}
